<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/25
 * Time: 下午9:26
 */

namespace common\logic;


use common\models\AdminModel;
use yii\db\ActiveQuery;

class AdminUserLogic extends Logic
{

    /**
     * @return ActiveQuery
     */
    protected function _getActiveQuery()
    {
        return AdminModel::find();
    }

    public function updateLoginTime($user_id)
    {
        return $this->update(['login_time' => time()], ['id' => $user_id]);
    }

    public function delete($user_id){
        return $this->update(['status'=>AdminModel::STATUS_DELETED],['id' => $user_id]);
    }

    /**
     * @param $id
     *
     * @return array|null|AdminModel
     */
    public function getById($id){
        return $this->_get(['id' => $id]);
    }

    protected function update($set, $where)
    {
        try {
            AdminModel::updateAll($set, $where);

            return true;
        } catch (\Throwable $e) {
        }

        return false;
    }


    public function signup($post)
    {
        $user = new AdminModel();
        $user->load($post);
        $user->setPassword($post['AdminModel']['password_hash']);

        return $user->save();
    }
}